﻿using System;
using System.Linq;
using System.Text;
using System.Configuration;

using System.IO;

namespace LYCleanUp
{
    class Program
    {
        static void Main()
        {
            DeleteFiles();
            DeleteDirectories();

            Console.WriteLine("Press ENTER to exit!");
            Console.ReadLine();
        }

        private static void DeleteDirectories()
        {
            var files = GetDirectories();

            var query1 = from s in files
                         where s.ToLower().Contains("resharper")
                         select s;

            var query2 = from s in query1
                         where !s.ToLower().Contains("svn")
                         select s;


            StringBuilder sb = new StringBuilder();

            foreach (var file in query2)
            {
                sb.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt")).Append(file).AppendLine();
            }

            Console.Write(sb.ToString());

            //FileStream fs = new FileStream(Directory.GetCurrentDirectory() + "\\log.txt", FileMode.Append);
            //StreamWriter streamWriter = new StreamWriter(fs);
            //streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            //streamWriter.WriteLine(DateTime.Now.ToString(CultureInfo.CurrentCulture) + " " + sb);
            //streamWriter.Flush();

            foreach (var file in query2)
            {
                try
                {
                    Directory.Delete(file,true);
                }
                catch (Exception)
                {
                    
                }
                
            }
        }

        private static void DeleteFiles()
        {
            var files = GetFiles();

            var query1 = from s in files
                         where s.ToLower().Contains("debug")
                               || s.ToLower().Contains("release")
                               || s.ToLower().Contains("resharper")
                               || s.ToLower().Contains("resharper.user")
                         select s;

            var query2 = from s in query1
                         where !s.ToLower().Contains("svn")
                               && !s.ToLower().Contains("lycleanup.exe")
                               && !s.ToLower().Contains("lycleanup.pdb")
                               && !s.ToLower().Contains("lycleanup.vshost.exe")
                               && !s.ToLower().Contains("lycleanup.vshost.exe.manifest")
                               && !s.ToLower().Contains("lycleanup.vshost.exe.config")
                         select s;

            StringBuilder sb = new StringBuilder();

            foreach (var file in query2)
            {
                sb.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt")).Append(file).AppendLine();
            }

            Console.Write(sb.ToString());

            //FileStream fs = new FileStream(Directory.GetCurrentDirectory() + "\\log.txt", FileMode.Create);
            //StreamWriter streamWriter = new StreamWriter(fs);
            //streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            //streamWriter.WriteLine(DateTime.Now.ToString(CultureInfo.CurrentCulture) + " " + sb);
            //streamWriter.Flush();

            foreach (var file in query2)
            {
                File.Delete(file);
            }
        }

        public static string[] GetDirectories()
        {
            var rootDir = ConfigurationManager.AppSettings["RootDir"];
            
            var files = Directory.GetDirectories(rootDir, "*", SearchOption.AllDirectories);

            return files;
        }

        public static string[] GetFiles()
        {
            var rootDir = ConfigurationManager.AppSettings["RootDir"];

            var files = Directory.GetFiles(rootDir , "*.*", SearchOption.AllDirectories);

            return files;
        }
    
    }
}
